package cn.sh.ideal.web.login.menu.shiro;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import cn.sh.ideal.web.login.menu.dao.MenuMapper;
import cn.sh.ideal.web.login.menu.entity.MenuEntity;
import cn.sh.ideal.web.login.menu.vo.ValidateLoginVo;


public class MyShiroRealm extends AuthorizingRealm{
	
	@Autowired
	private MenuMapper menuMapper;
	
	
    /*	
    private static final String USER_NAME = "luoguohui";  
	private static final String PASSWORD = "123456"; 
	*/
	
	/* 
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 
       /* Set<String> roleNames = new HashSet<String>();  
        Set<String> permissions = new HashSet<String>();  
        roleNames.add("administrator");//添加角色
        permissions.add("newPage.jhtml");  //添加权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);  
        info.setStringPermissions(permissions);  
        return info;  */
		return null;
	}
	
	
	


	/* 
	 * 登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		MenuEntity loginerPamater = new MenuEntity();
		loginerPamater.setLoginUser(token.getUsername());
	    loginerPamater.setLoginPasswd(new String(token.getPassword()));
		
		ValidateLoginVo result = menuMapper.checkLoginNmAndPasswd(loginerPamater);
		result.getUserName();
		
	if(result.getUserName() != null){
		
			return new SimpleAuthenticationInfo(result, result.getUserPasswd(), getName());  
		}else{
			throw new AuthenticationException();  
		}
	}



	
	
	
}
